package dao;

import dao.base.BaseDao;
import model.Order;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 用户Dao层 查询和批量新增方法使用了hikari连接池
 */
public class OrderDao extends BaseDao<Order> {
    /**
     * 查询
     */
    @Override
    public List<Order> selectAll() {
        return Collections.EMPTY_LIST;
    }

    static final String insertOrderSql = "insert into trade_0(id,trade_no,user_id,payment,payment_type," +
            "status,payment_time,close_time,create_time,update_time)values(?,?,?,?,?,?,?,?,?,?)";

    static final String batchInertPartSql = ",(?,?,?,?,?,?,?,?,?,?)";

    /**
     * 新增
     * 使用hikari连接池
     */
    @Override
    public int insert(Order order) {
        Connection conn = getHikariConn();
        try {
            PreparedStatement ps = conn.prepareStatement(insertOrderSql);
            ps.setObject(1, order.getId());
            ps.setObject(2, order.getTradeNo());
            ps.setObject(3, order.getUserId());
            ps.setObject(4, order.getPayment());
            ps.setObject(5, order.getPaymentType());
            ps.setObject(6, order.getStatus());
            ps.setObject(7, order.getPaymentTime());
            ps.setObject(8, order.getCloseTime());
            ps.setObject(9, order.getCreateTime());
            ps.setObject(10, order.getUpdateTime());
            int success = ps.executeUpdate();
            return success;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 批量新增
     */
    @Override
    public int batchInsert(List<Order> userList) {
        if (userList == null || userList.size() == 0) {
            return 0;
        }
        String sql = insertOrderSql;
        Connection conn = getHikariConn();
        try {
            //批量新增不自动提交
            conn.setAutoCommit(false);

            PreparedStatement ps = conn.prepareStatement(sql);
            Date now = new Date();
            int size = userList.size();
            for (int i = 0; i < size; i++) {
                Order order = userList.get(i);
                ps.setObject(1, order.getId());
                ps.setObject(2, order.getTradeNo());
                ps.setObject(3, order.getUserId());
                ps.setObject(4, order.getPayment());
                ps.setObject(5, order.getPaymentType());
                ps.setObject(6, order.getStatus());
                ps.setObject(7, order.getPaymentTime());
                ps.setObject(8, order.getCloseTime());
                ps.setObject(9, order.getCreateTime());
                ps.setObject(10, order.getUpdateTime());
                ps.addBatch();
            }
            ps.executeBatch();
            ps.clearBatch();
            conn.commit();
            return userList.size();
        } catch (Exception e) {
            try {
                conn.rollback();
                throw new RuntimeException(e);
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }


    /**
     * 批量新增
     */
    public int batchInsert2(List<Order> userList) {
        if (userList == null || userList.size() == 0) {
            return 0;
        }
        String sql = insertOrderSql;
        Connection conn = getHikariConn();
        try {
            //不自动提交
            conn.setAutoCommit(false);
            if (userList.size() > 1) {
                StringBuilder sb = new StringBuilder(sql);
                int size = userList.size();
                for (int i = 0; i < size - 1; ++i) {
                    sb.append(batchInertPartSql);
                }
                sql = sb.toString();
            }
            PreparedStatement ps = conn.prepareStatement(sql);
            int loop = userList.size() - 1;
            for (int i = 0; i <= loop; i++) {
                Order order = userList.get(i);
                ps.setObject(1 + i * 10, order.getId());
                ps.setObject(2 + i * 10, order.getTradeNo());
                ps.setObject(3 + i * 10, order.getUserId());
                ps.setObject(4 + i * 10, order.getPayment());
                ps.setObject(5 + i * 10, order.getPaymentType());
                ps.setObject(6 + i * 10, order.getStatus());
                ps.setObject(7 + i * 10, order.getPaymentTime());
                ps.setObject(8 + i * 10, order.getCloseTime());
                ps.setObject(9 + i * 10, order.getCreateTime());
                ps.setObject(10 + i * 10, order.getUpdateTime());
            }
            ps.executeUpdate();
            conn.commit();
            return userList.size();
        } catch (Exception e) {
            try {
                conn.rollback();
                throw new RuntimeException(e);
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }


    /**
     * 修改
     * 根据用户id修改用户的username
     */
    @Override
    public int updateByPrimaryKey(Order Order) {
        return -1;
    }

    /**
     * 删除
     * 根据用户id删除
     */
    @Override
    public int deleteByPrimaryKey(Order Order) {
        return -1;
    }

    public void truncate() {
        Connection conn = getHikariConn();
        try {
            PreparedStatement ps = conn.prepareStatement("truncate trade_0");
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
